package com.uchengwang.functions.db.manager;

import android.content.Context;

import com.litesuits.orm.db.model.ColumnsValue;
import com.uchengwang.ui.cm_ui.umsg.bean.WantChatBean;

import java.util.List;

/**
 * Created by U城网 on 2017/12/22.
 */

public class WantCHatDBManage {
    private static WantCHatDBManage manage;
    private WantCHatDBManage (){}

    private List<WantChatBean> list;

    public static WantCHatDBManage getManage() {
        if (manage ==null){
            manage = new WantCHatDBManage();
        }
        return manage;
    }

    /**
     * 获取享聊的所有记录 判断一个人是否在
     * */
    public boolean isInWantChat(Context context, long uid){
        list = LiteOrmDBUtil.getLiteOrm(context).getQueryAll(WantChatBean.class);
        if (list != null && list.size() >0){
            for (WantChatBean bean : list){
                if (bean.getUid() == uid){
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * 判断有没有给这个人发送享聊信息, 或者时间超过10分钟
     * 返回数值 0代表存在 并且时间没超过规定数值  1代表存在 但是时间超过10分钟 会更新数据库 发送信息 2表示不存在 会插入进数据库 发送信息
     * 大于等于10分钟 再次发送小灰条
     * */
    public int isSend(Context context, WantChatBean bean){
        if (bean ==null){
            return 0;
        }
        List<WantChatBean> list = LiteOrmDBUtil.getLiteOrm(context).getQueryAll(WantChatBean.class);
        if (list != null && list.size() >0){
            for (WantChatBean b: list){
                if (bean.getUid() ==b.getUid()){
                    long time = b.getSendMsgTime();
                    if ((System.currentTimeMillis() -time)/1000/60 >=10){
                        //更新当前时间进去
                        ColumnsValue value = new ColumnsValue(new String[]{"sendMsgTime"}, new Object[]{System.currentTimeMillis()});
                        LiteOrmDBUtil.getLiteOrm(context).update(WantChatBean.class, value, "uid", b.getUid());
                        return 1;
                    }else {
                        return 0;
                    }
                }
            }
        }
        bean.setSendMsgTime(System.currentTimeMillis());
        LiteOrmDBUtil.getLiteOrm(context).insert(bean);
        return 2;
    }
}
